﻿#include "orderformcomment.h"
#include "ui_orderformcomment.h"
#include <QFileDialog>
#include <QDir>
#include <QDebug>
#include <QDate>
#include <QMessageBox>

OrderFormComment::OrderFormComment(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OrderFormComment)
{
    ui->setupUi(this);

    m_star = 0;
    m_haveImage = true;
    m_commentWord = false;
    ui->pb_submit->setEnabled(false);
    ui->pb_submit->setStyleSheet("background-color: rgb(118, 118, 118);");
}

OrderFormComment::~OrderFormComment()
{
    delete ui;
}

void OrderFormComment::setResult(const QString& msg)
{
    QMessageBox::warning(this,
                 "结果",
                 msg,
                 QMessageBox::Ok,
                 QMessageBox::Ok);
}

void OrderFormComment::setHotel(const QStringList &strList)
{
    ui->lb_hotelName->setText(strList.at(1));
    m_commentInfo.setHotel(strList.at(0));
    m_commentInfo.setHotelName(strList.at(1));
    m_commentInfo.setHouseType(strList.at(2));
    m_commentInfo.setCheckInTime(strList.at(3));
    m_commentInfo.setReserveID(strList.at(4));
}

void OrderFormComment::judgeSubmit()
{
    if (m_commentWord)
    {
        ui->pb_submit->setEnabled(true);
        ui->pb_submit->setStyleSheet("background-color: rgb(255, 85, 0);");
    }else
    {
        ui->pb_submit->setEnabled(false);
        ui->pb_submit->setStyleSheet("background-color: rgb(118, 118, 118);");
    }
}
void OrderFormComment::on_pb_comment1_clicked()
{
    ui->lb_star1->setStyleSheet("color: rgb(255, 85, 0);");
    ui->lb_star2->setStyleSheet("");
    ui->lb_star3->setStyleSheet("");
    ui->lb_star4->setStyleSheet("");
    ui->lb_star5->setStyleSheet("");
    ui->pb_comment1->setStyleSheet("border-image: url(:/images/comment/comment1_01.png);");
    ui->pb_comment2->setStyleSheet("border-image: url(:/images/comment/no_comment2_03.png);");
    ui->pb_comment3->setStyleSheet("border-image: url(:/images/comment/no_comment3_05.png);");
    ui->pb_comment4->setStyleSheet("border-image: url(:/images/comment/no_comment4_10.png);");
    ui->pb_comment5->setStyleSheet("border-image: url(:/images/comment/no_comment5_12.png);");

    m_star = 1;
    judgeSubmit();
}
void OrderFormComment::on_pb_comment2_clicked()
{
    ui->lb_star1->setStyleSheet("");
    ui->lb_star2->setStyleSheet("color: rgb(255, 85, 0);");
    ui->lb_star3->setStyleSheet("");
    ui->lb_star4->setStyleSheet("");
    ui->lb_star5->setStyleSheet("");
    ui->pb_comment1->setStyleSheet("border-image: url(:/images/comment/no_comment1_01.png);");
    ui->pb_comment2->setStyleSheet("border-image: url(:/images/comment/comment2_03.png);");
    ui->pb_comment3->setStyleSheet("border-image: url(:/images/comment/no_comment3_05.png);");
    ui->pb_comment4->setStyleSheet("border-image: url(:/images/comment/no_comment4_10.png);");
    ui->pb_comment5->setStyleSheet("border-image: url(:/images/comment/no_comment5_12.png);");

    m_star = 2;
    judgeSubmit();
}
void OrderFormComment::on_pb_comment3_clicked()
{
    ui->lb_star1->setStyleSheet("");
    ui->lb_star2->setStyleSheet("");
    ui->lb_star3->setStyleSheet("color: rgb(255, 85, 0);");
    ui->lb_star4->setStyleSheet("");
    ui->lb_star5->setStyleSheet("");
    ui->pb_comment1->setStyleSheet("border-image: url(:/images/comment/no_comment1_01.png);");
    ui->pb_comment2->setStyleSheet("border-image: url(:/images/comment/no_comment2_03.png);");
    ui->pb_comment3->setStyleSheet("border-image: url(:/images/comment/comment3_05.png);");
    ui->pb_comment4->setStyleSheet("border-image: url(:/images/comment/no_comment4_10.png);");
    ui->pb_comment5->setStyleSheet("border-image: url(:/images/comment/no_comment5_12.png);");

    m_star = 3;
    judgeSubmit();
}
void OrderFormComment::on_pb_comment4_clicked()
{
    ui->lb_star1->setStyleSheet("");
    ui->lb_star2->setStyleSheet("");
    ui->lb_star3->setStyleSheet("");
    ui->lb_star4->setStyleSheet("color: rgb(255, 85, 0);");
    ui->lb_star5->setStyleSheet("");
    ui->pb_comment1->setStyleSheet("border-image: url(:/images/comment/no_comment1_01.png);");
    ui->pb_comment2->setStyleSheet("border-image: url(:/images/comment/no_comment2_03.png);");
    ui->pb_comment3->setStyleSheet("border-image: url(:/images/comment/no_comment3_05.png);");
    ui->pb_comment4->setStyleSheet("border-image: url(:/images/comment/comment4_10.png);");
    ui->pb_comment5->setStyleSheet("border-image: url(:/images/comment/no_comment5_12.png);");

    m_star = 4;
    judgeSubmit();
}
void OrderFormComment::on_pb_comment5_clicked()
{
    ui->lb_star1->setStyleSheet("");
    ui->lb_star2->setStyleSheet("");
    ui->lb_star3->setStyleSheet("");
    ui->lb_star4->setStyleSheet("");
    ui->lb_star5->setStyleSheet("color: rgb(255, 85, 0);");
    ui->pb_comment1->setStyleSheet("border-image: url(:/images/comment/no_comment1_01.png);");
    ui->pb_comment2->setStyleSheet("border-image: url(:/images/comment/no_comment2_03.png);");
    ui->pb_comment3->setStyleSheet("border-image: url(:/images/comment/no_comment3_05.png);");
    ui->pb_comment4->setStyleSheet("border-image: url(:/images/comment/no_comment4_10.png);");
    ui->pb_comment5->setStyleSheet("border-image: url(:/images/comment/comment5_12.png);");

    m_star = 5;
    judgeSubmit();
}

void OrderFormComment::on_pb_uploadImages_clicked()
{
    m_imagePathList = QFileDialog::getOpenFileNames(this, tr("Open Image"),
                                                            "F:\\ForWork\\HotelPlatform\\image\\head_pic\\avatar",
                                                            tr("Image (*.png *.jpg)"));

    if (m_imagePathList.size() == 0)
    {
        return;
    }

    ui->lb_image1->clear();
    ui->lb_image2->clear();
    ui->lb_image3->clear();
    ui->lb_image4->clear();
    ui->lb_image5->clear();
    switch (m_imagePathList.size())
    {
        case 5:
            ui->lb_image5->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(4)).scaled(100,100)));
        case 4:
            ui->lb_image4->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(3)).scaled(100,100)));
        case 3:
            ui->lb_image3->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(2)).scaled(100,100)));
        case 2:
            ui->lb_image2->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(1)).scaled(100,100)));
        case 1:
        {
            ui->lb_image1->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(0)).scaled(100,100)));
            m_haveImage = true;
        } break;
        default:
        {
            ui->lb_image1->setText("至多上传5张图片");
            m_haveImage = false;
        } break;
    }
}

void OrderFormComment::on_pb_submit_clicked()
{
    m_commentInfo.setComment(QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));
    m_commentInfo.setUser(GlobalVars::g_localUser.getNumber());
    m_commentInfo.setStar(QString::number(m_star));
    m_commentInfo.setCommentTime(QDate::currentDate().toString("yyyy-MM-dd"));
    m_commentInfo.setUserComment(m_comment);
    m_commentInfo.setPhoto(m_imagePathList.join("#"));

    GlobalVars::g_localComment = m_commentInfo;
    emit signalCommentSubmit();
}

void OrderFormComment::on_te_comment_textChanged()
{
    m_comment = ui->te_comment->document()->toPlainText();
    qDebug() << m_comment;
    if (m_comment.isEmpty())
    {
        m_commentWord = false;
        ui->pb_submit->setEnabled(false);
        ui->pb_submit->setStyleSheet("background-color: rgb(118, 118, 118);");
    }
    else
    {
        QRegExp reg_exp("^.{20,120}$");///
        QRegExpValidator *validator = new QRegExpValidator(reg_exp);
        if(!validator->regExp().exactMatch(m_comment))/// 匹配不对
        {
            qDebug() << "comment  no";
            m_commentWord = false;
            ui->pb_submit->setEnabled(false);
            ui->pb_submit->setStyleSheet("background-color: rgb(118, 118, 118);");
        }
        else
        {
            qDebug() << "comment  yes";
            m_commentWord = true;
            if (m_star != 0)
            {
                ui->pb_submit->setEnabled(true);
                ui->pb_submit->setStyleSheet("background-color: rgb(255, 85, 0);");
            }
        }
    }
}
